﻿<template>
	<a-row>
		<a-col :span="5">
			<a-card class="cardImp" :bordered="false" :loading="cardLoading">
				<a-tree
					v-if="treeData.length > 0"
					v-model:expandedKeys="defaultExpandedKeys"
					:tree-data="treeData"
					:field-names="treeFieldNames"
					@select="treeSelect"
				/>
				<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
			</a-card>
		</a-col>
		<a-col :span="19">
			<a-card :bordered="false" style="margin-bottom: 10px">
				<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
					<a-row :gutter="24">
						<a-col :span="6">
							<a-form-item name="name" label="姓名">
								<a-input v-model:value="searchFormState.name" placeholder="请输入姓名"></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-button type="primary" @click="table.refresh(true)">
								<template #icon>
									<SearchOutlined />
								</template>
								查询
							</a-button>
							<a-button class="buttom-left" @click="() => searchFormRef.resetFields()">
								<template #icon>
									<redo-outlined />
								</template>
								重置
							</a-button>
						</a-col>
					</a-row>
				</a-form>
			</a-card>
			<a-card :bordered="false">
				<s-table
					ref="table"
					:columns="columns"
					:data="loadData"
					:alert="options.alert.show"
					:scroll="{ x: '1000px' | true }"
					bordered
					:row-key="(record) => record.id"
					:row-selection="options.rowSelection"
					:tool-config="toolConfig"
				>
					<template #operator>
						<a-space>
							<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('genTestAdd')">
								<template #icon>
									<plus-outlined />
								</template>
								新增测试
							</a-button>
							<a-button @click="openImport" v-if="hasPerm('genTestImport')">
								<template #icon>
									<download-outlined />
								</template>
								导入测试
							</a-button>
							<data-export buttonName="导出测试" @dataExport="dataExportgenTest" v-if="hasPerm('genTestExport')" />
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'sex'">
							{{ $TOOL.dictTypeData('GENDER', record.sex) }}
						</template>
						<template v-if="column.dataIndex === 'nation'">
							{{ $TOOL.dictTypeData('NATION', record.nation) }}
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a @click="formRef.onOpen(record)" v-if="hasPerm('genTestEdit')">编辑</a>
								<a-divider type="vertical" v-if="hasPerm(['genTestEdit', 'genTestDelete'], 'and')" />
								<a-popconfirm title="确定要删除吗？" @confirm="deleteGenTest(record)">
									<a-button type="link" danger size="small" v-if="hasPerm('genTestDelete')">删除 </a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-card>
			<Form ref="formRef" @successful="table.refresh(true)" />
			<data-import
				ref="importRef"
				title="导入测试"
				:api="genTestApi"
				:max-upload-count="2000"
				@successful="table.refresh(true)"
			/>
		</a-col>
	</a-row>
</template>

<script setup name="test">
	import { message, Empty } from 'ant-design-vue'
	import Form from './form.vue'
	import genTestApi from '@/api/biz/genTestApi'
	import dataImport from '@/components/Import/dataImport/index.vue'

	let searchFormState = reactive({}) //查询表单状态
	const searchFormRef = ref() //查询表单
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const formRef = ref() //新增/编辑表单页

	//左侧树配置
	let defaultExpandedKeys = ref([]) // 默认展开的节点
	const treeData = ref([]) // 左侧树数据
	// 替换treeNode 中 title,key,children
	const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
	const cardLoading = ref(true) // 左侧树加载状态

	const importRef = ref() //导入组件

	//表格配置
	const table = ref() //表格
	let selectedRowKeys = ref([]) //选中项
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false } //表格工具配置
	const columns = [
		{
			title: '姓名',
			width: 100,
			dataIndex: 'name'
		},
		{
			title: '性别',
			width: 100,
			dataIndex: 'sex'
		},
		{
			title: '民族',
			width: 100,
			dataIndex: 'nation'
		},
		{
			title: '年龄',
			width: 100,
			dataIndex: 'age'
		},
		{
			title: '生日',
			width: 100,
			dataIndex: 'bir'
		},
		{
			title: '排序码',
			width: 100,
			dataIndex: 'sortCode'
		},
		{
			title: '存款',
			width: 100,
			dataIndex: 'money'
		}
	]
	if (hasPerm(['genTestEdit', 'genTestDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right',
			width: '130px'
		})
	}

	// 列表选择配置
	const options = reactive({
		alert: {
			show: true, //显示选择多少个
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	})

	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return genTestApi.testPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}

	// 删除
	const deleteGenTest = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		genTestApi.testDelete(params).then(() => {
			table.value.refresh(true)
		})
	}

	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys.length > 0) {
			searchFormState.orgId = selectedKeys.toString()
		} else {
			delete searchFormState.orgId
		}
		table.value.refresh(true)
	}
	// 左侧树查询
	genTestApi
		.orgTree()
		.then((res) => {
			cardLoading.value = false
			if (res !== null) {
				treeData.value = res
				// 默认展开2级
				treeData.value.forEach((item) => {
					// 因为0的顶级
					if (item.parentId === 0) {
						defaultExpandedKeys.value.push(item.id)
						// 取到下级ID
						if (item.children) {
							item.children.forEach((items) => {
								defaultExpandedKeys.value.push(items.id)
							})
						}
					}
				})
			}
		})
		.finally(() => {
			cardLoading.value = false
		})

	// 打开导入界面
	const openImport = (record) => {
		nextTick(() => {
			importRef.value.importSteps(record)
		})
	}
	// 导出数据
	const dataExportgenTest = (callback) => {
		genTestApi
			.testExport(searchFormState)
			.then((data) => {
				callback(data)
			})
			.catch(() => {
				callback(null)
			})
	}
</script>

<style scoped>
	.cardImp {
		margin-right: 10px;
	}

	.buttom-left {
		margin-left: 8px;
	}
</style>
